//
//  MeatMessageViewController.m
//  PigMarket
//
//  Created by 张敬 on 2019/8/26.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "MeatMessageViewController.h"
#import "MessageParam.h"
#import "MeatMarketApi.h"



@interface MeatMessageViewController ()<UITextViewDelegate>

@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) UILabel *placeHolderLab;
@property (nonatomic, strong) UIButton *sumbitBtn;
@property (nonatomic, strong) UIImageView *cleanIcon;

@property (nonatomic, strong) MessageParam *param;
@property (nonatomic, strong) MeatMarketApi *meatApi;


@end

@implementation MeatMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.baseNavView.navTitleLabel.text = @"店铺留言";
    [self setupUI];
    [self setupLayout];
    
}

- (void)setupUI{
    
    {
        [self.view addSubview:self.label];
        [self.view addSubview:self.textView];
        [self.view addSubview:self.sumbitBtn];
    }
    

}

- (void)setupLayout{
    
    {
        _label.size = CGSizeMake(KScreenWidth - 30, 34);
        _label.mj_y = TJ_NavBarHeight;
        _label.mj_x = 15;
    }
    
    {
        _textView.mj_y = _label.bottom;
    }
    
    {
        _cleanIcon.size = CGSizeMake(15, 15);
        _cleanIcon.mj_y = (_textView.height - _cleanIcon.height)/2.0;
        _cleanIcon.mj_x = KScreenWidth - _cleanIcon.width - 15;
    }
    
    {
        _sumbitBtn.size =CGSizeMake(KScreenWidth - 40, 48);
        _sumbitBtn.mj_y = _textView.bottom + 90;
        _sumbitBtn.mj_x = 20;
    }
    
}

#pragma mark - networking methods
- (void)sumbitMsgContent{
    @weakify(self);
    [self showProgressView];
    [[self.meatApi getMeatShopMessageByParams:self.param] subscribeNext:^(NSNull * _Nullable x) {
        @strongify(self);
        [self hideProgressView];
        [self.navigationController popViewControllerAnimated:YES];
    }error:^(NSError * _Nullable error) {
        @strongify(self);
        [self hideProgressView];
        [self showHudWithMsg:@"留言失败"];
    }];
}

#pragma mark - delegate

- (void)textViewDidBeginEditing:(UITextView *)textView{
    _placeHolderLab.hidden = YES;
}


- (void)textViewDidChange:(UITextView *)textView {
    
    [self updateRemainCount];
}


- (void)updateRemainCount {
    
    NSInteger count = self.textView.text.length;
    if (count <=0) {
        _placeHolderLab.hidden = NO;
    }else{
        _placeHolderLab.hidden = YES;
    }
}


#pragma mark - getter methods

- (UILabel *)label{
    
    if (!_label) {
        _label = [UILabel makeLabelWithText:@"写下想对店家说的，留下联系方式，等待卖家联系" textColor:UIColorFromRGB(0x969696) font:mFont(14) textAliment:0];
    }
    return _label;
}


- (UITextView *)textView{
    if (!_textView) {
        _textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 54)];
        _textView.font = mFont(13);
        _textView.textColor = UIColorFromRGB(0x3333333);
        _textView.editable = YES;
        _textView.userInteractionEnabled = YES; ///
        _textView.delegate = self;
        _textView.scrollEnabled = YES;//滑动
        _textView.returnKeyType = UIReturnKeyDone;//返回键类型
        _textView.keyboardType = UIKeyboardTypeDefault;//键盘类型
        _textView.textContainerInset = UIEdgeInsetsMake(17, 15, 17, 30);
        
        [_textView addSubview:self.placeHolderLab];
        _placeHolderLab.mj_x = 15;
        _placeHolderLab.mj_y = 17;
        
        [_textView addSubview:self.cleanIcon];
    }
    return _textView;
}


- (UILabel *)placeHolderLab{
    
    if (!_placeHolderLab) {
        _placeHolderLab = [[UILabel alloc] init];
        _placeHolderLab.text = @"请输入您的留言～";
        _placeHolderLab.textColor = UIColorFromRGB(0x999999);
        _placeHolderLab.font = mFont(13);
        _placeHolderLab.textAlignment = NSTextAlignmentLeft;
        _placeHolderLab.size = CGSizeMake(200, 20);
    }
    return _placeHolderLab;
}


- (UIImageView *)cleanIcon{
    
    if (!_cleanIcon) {
        _cleanIcon = [[UIImageView alloc] init];
        _cleanIcon.image = [UIImage imageNamed:@"meat_message_clean"];
        
        _cleanIcon.isNeedAnimation = YES;
        
        @weakify(self);
        _cleanIcon.tapBlock = ^{
            @strongify(self);
            self.textView.text = @"";
            self.textView.height = 54;
            self.cleanIcon.mj_y = (self.textView.height - self.cleanIcon.height)/2.0;
            self.sumbitBtn.mj_y = self.textView.bottom + 90;
            self.placeHolderLab.hidden = NO;

        };
    }
    return _cleanIcon;
}


- (UIButton *)sumbitBtn{
    
    if (!_sumbitBtn) {
        _sumbitBtn = [UIButton makeButtonWithText:@"发送" textColor:KWhiteColor font:mFont(14) textAliment:1 cornerRadius:5];
        [_sumbitBtn setBackgroundImage:[UIImage imageNamed:@"meat_cart_pay_right"] forState:UIControlStateNormal];
        
        @weakify(self);
        _sumbitBtn.tapBlock = ^{
            @strongify(self);
            [self sumbitMsgContent];
        };
    }
    return _sumbitBtn;
}

- (MessageParam *)param{
    
    if (!_param) {
        _param = [[MessageParam alloc] init];
        _param.shopId = self.shopId;
        _param.memberId = kMemberId;
        _param.modeType = @1;
    }
    return _param;
}

- (MeatMarketApi *)meatApi{
    
    if (!_meatApi) {
        _meatApi = [[MeatMarketApi alloc] init];
    }
    return _meatApi;
}


#pragma mark - notification methods


- (void)textChanged:(NSNotification *)notification{
    
    CGFloat textViewHeight = self.textView.contentSize.height;
    if (textViewHeight >54) {
        _textView.height = textViewHeight;
    }else{
        _textView.height = 54;
    }
    _cleanIcon.mj_y = (_textView.height - _cleanIcon.height)/2.0;
    _sumbitBtn.mj_y = _textView.bottom + 90;
    
    UITextView *aimTextView = notification.object;
    self.param.msgContent = aimTextView.text;

}

- (void)addNotifications{
    
    [kNotificationCenter addObserver:self selector:@selector(textChanged:) name: UITextViewTextDidChangeNotification object:nil];
}

- (void)removeNotifications{
    
    [kNotificationCenter removeObserver:self name:UITextViewTextDidChangeNotification object:nil];
}



@end
